//
//  NDOrderListViewModel.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/26.
//

import Foundation

enum NDOrderDataType {
    case all
    case applying
    case repayment
    case finish
    
    var title: String {
        switch self {
        case .all:
            return localizedString("All")
        case .applying:
            return localizedString("Applying")
        case .repayment:
            return localizedString("Repayment")
        case .finish:
            return localizedString("Finish")
        }
    }
    
    var tag: String {
        switch self {
        case .all:
            return "4"
        case .applying:
            return "7"
        case .repayment:
            return "6"
        case .finish:
            return "5"
        }
    }
}

class NDOrderListViewModel: ObservableObject {
    
    @Published var productListModel: ProductListModel?
    @Published var isLoading = false
    @Published var errorMessage: String?
    @Published var refresh = false
    
    @Published var orderDataType: NDOrderDataType = .all {
        didSet {
            refresh = true
        }
    }
    
    func loadOrderList() {
        
        isLoading = true
        errorMessage = nil
        productListModel = nil
        NDRequestManager.requestProductList(orderListSelect: orderDataType) { [weak self] model, msg, error in
            guard let self = self else { return }
            DispatchQueue.main.async {
                self.isLoading = false
                self.refresh = false
                
                if let error = error {
                    self.errorMessage = msg
                    print("请求失败: \(error)")
                    return
                }
                
                guard let model = model else {
                    self.errorMessage = "failed"
                    return
                }
                
                self.productListModel = model
            }
        }
    }
}

enum OrderType: String, RawRepresentable {
    case paymentSuccess = "sexualular" // 放款成功
    case overdue = "emetoess" // 逾期
    case applying = "causule" // 申请中
    case termLoaning = "clysthalf" // 审或者放款中
    case finished = "palety" // 订单完成

    var backgroundColorHex: String {
        switch self {
        case .paymentSuccess:
            return "#FD632B"
        case .overdue:
            return "#FD632B"
        case .applying:
            return "#00B42A"
        case .termLoaning:
            return "#999999"
        case .finished:
            return "#999999"
        }
    }
    
    var describeColorHex: String {
        switch self {
        case .paymentSuccess:
            return "#1677FF"
        case .overdue:
            return "#FF4D4F"
        case .applying:
            return "#00B42A"
        case .termLoaning:
            return "#9254DE"
        case .finished:
            return "#999999"
        }
    }
}

